import React from 'react';
import { Modal, message } from 'antd';
import { WeaTools, WeaLocaleProvider, WeaDialog, WeaRightMenu, WeaMoreButton, WeaTab, WeaButtonIcon } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
import { WeaTableNew } from 'comsMobx';

const { WeaTable, TableStore } = WeaTableNew;
const tableStore = new TableStore();

import './style/';
import Add from './Add';

class WeaPortalShare extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: props.visible || false,
            title: props.title || getLabel(82752, '共享范围'),
            type: props.type || {},
            selectedRowKeys: [],
        };
        this.getButtons = this.getButtons.bind(this);
        this.getRightMenus = this.getRightMenus.bind(this);
        this.getRowSelection = this.getRowSelection.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.onLoad = this.onLoad.bind(this);
        this.onAdd = this.onAdd.bind(this);
        this.onSave = this.onSave.bind(this);
        this.onDelete = this.onDelete.bind(this);
    }

    render() {
        const { visible, title, type, selectedRowKeys: keys } = this.state;
        const disabled = !(keys && keys.length);

        return (
            <React.Fragment ecId={`${this && this.props && this.props.ecId || ''}_React.Fragment@bu0qe2`}>
                <div className="wea-portal-share" onClick={() => this.onLoad(type)}>
                    {this.props.children}
                </div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@4pn2rw`}
                  visible={visible}
                  title={title}
                  icon="icon-coms-portal"
                  iconBgcolor="#1a57a0"
                  style={{ width: 800, height: 510 }}
                  hasScroll={true}
                  buttons={this.getButtons()}
                  onCancel={this.onCancel}
                >
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@x5i6ej`} datas={this.getRightMenus()}>
                        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@7fp7ze`}
                          buttons={[
                                <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@qphu8c`} buttonType="add" type="primary" onClick={this.onAdd} />,
                                <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@5ruicf`} buttonType="del" type="primary" disabled={disabled} onClick={this.onDelete} />,
                            ]}
                        />
                        <Add ecId={`${this && this.props && this.props.ecId || ''}_Add@kszf1i`} ref={add => this.addRef = add} title={getLabel(611, '添加') + title} onSave={this.onSave} />
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@swrwaw`} comsWeaTableStore={tableStore} rowSelection={this.getRowSelection()} scroll={{ y: 380 }} />
                    </WeaRightMenu>
                </WeaDialog>
            </React.Fragment>
        );
    }

    getButtons() {
        let buttons = [];
        buttons.push(<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@4i51km`} datas={this.getRightMenus()} />);
        return buttons;
    }

    getRightMenus() {
        const { selectedRowKeys: keys } = this.state;

        let rightMenus = [];
        rightMenus.push({ key: '1', icon: <i className="icon-coms-Add-to-o" />, content: getLabel(611, '添加'), onClick: this.onAdd });
        rightMenus.push({ key: '2', disabled: !(keys && keys.length), icon: <i className="icon-coms-Batch-delete" />, content: getLabel(32136, '批量删除'), onClick: this.onDelete });
        return rightMenus;
    }

    getRowSelection() {
        const _self = this;
        return {
            onChange(selectedRowKeys) {
                _self.setState({ selectedRowKeys });
            },
        };
    }

    onCancel() {
        const { onCancelCallback } = this.props;
        this.setState({ visible: false, selectedRowKeys: [] });
        tableStore.setSelectedRowKeys([]);
        tableStore.tableUpdate({ datas: [] });
        if (onCancelCallback) {
            onCancelCallback();
        }
    }

    onLoad(type, title = this.state.title) {
        this.setState({ visible: true, title, type });
        WeaTools.callApi('/api/portal/customshare/getCustomShareData', 'POST', {
            ...type,
        }).then((result) => {
            tableStore.getDatas(result.sessionkey, 1);
            this.setState({ selectedRowKeys: [] });
        });
    }

    onAdd() {
        this.addRef.onLoad();
    }

    onSave(data = {}) {
        const { type } = this.state;

        WeaTools.callApi('/api/portal/customshare/setCustomShareData', 'POST', {
            ...type,
            ...data,
        }).then((result) => {
            if (result.status === 'success') {
                message.success(getLabel(83885, '操作成功！'));
                this.addRef.onCancel();
                this.onLoad(type);
                const callback = this.props.callback;
                if (callback && typeof callback == 'function') {
                    callback();
                }
            } else {
                message.warning(getLabel(83912, '操作失败！'));
            }
        });
    }

    onDelete() {
        const { type, selectedRowKeys = [] } = this.state;

        Modal.confirm({
            title: getLabel(15172, '系统提示'),
            content: getLabel(385625, '确定要删除选择的记录吗？'),
            onOk: () => {
                WeaTools.callApi('/api/portal/customshare/delCustomShareData', 'POST', {
                    ids: selectedRowKeys.join(','),
                    ...type,
                }).then((result) => {
                    if (result.status === 'success') {
                        message.success(getLabel(83885, '操作成功！'));
                        this.onLoad(type);
                        const callback = this.props.callback;
                        if (callback && typeof callback == 'function') {
                            callback();
                        }
                    } else {
                        message.warning(getLabel(83912, '操作失败！'));
                    }
                });
            },
        });
    }
}

export default WeaPortalShare;
